<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import * as echarts from "echarts";
import resize from "../static/resize";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "30%",
    },
    height: {
      type: String,
      default: "100%",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        backgroundColor: "#2f4989",
        title: {
          text: "设备状态",
          top: "top",
          left: "center",
          textStyle: {
            color: "#fff",
            fontSize: "12",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        legend: {
          data: ["在线", "离线"],
          left: "center",
          bottom: "3%",
          textStyle: {
            color: "#90979c",
          },
        },
        calculable: true,
        series: [
          {
            name: "数量统计",
            type: "pie",
            barGap: "10%",
            itemStyle: {
              normal: {
                label: {
                  show: false,
                  textStyle: {
                    color: "#fff",
                    fontSize: "20",
                  },
                  // formatter: function (val) {
                  //   //让series 中的文字进行换行
                  //   return val.name.split("-").join("\n");
                  // },
                }, //饼图图形上的文本标签，可用于说明图形的一些数据信息，比如值，名称等。可以与itemStyle属性同级，具体看文档
                labelLine: {
                  show: false,
                  lineStyle: { color: "#3c4858" },
                }, //线条颜色
              }, //基本样式
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)", //鼠标放在区域边框颜色
                textColor: "#000",
              }, //鼠标放在各个区域的样式
            },
            data: [
              {
                value: 235,
                name: "在线",
              },
              { value: 274, name: "离线" },
            ],
            color: ["#4374c6", "#f27e30"], //各个区域颜色
            radius: [0, "60%"],
          },
        ],
      });
    },
  },
};
</script>
<style scoped>
</style>